package com.cefa.finance.controller;

import com.cefa.finance.model.Account;
import com.cefa.finance.result.ResultModel;
import com.cefa.finance.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("login")
@SuppressWarnings("SpringAutowiredFieldsWarningInspection")
public class LoginController {

    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(path = "web", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel login(Account source) {
        if (source.getUserAccount() == null || source.getPassword() == null) {
            return new ResultModel(-201, "用户名或者密码不能为空!");
        }
        try {
            Account account = loginService.getAccount(source);
            if (account == null) {
                return new ResultModel(-202, "用户名或者密码错误!");
            } else {
                return ResultModel.OK(account);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }
}
